//
//  RootviewConrtoller.m
//  007
//
//  Created by admin on 15/11/10.
//  Copyright (c) 2015年 REDOOR. All rights reserved.
//

#import "RootviewConrtoller.h"
#import "PlaceViewController.h"


//省市区三级联动进行操作
#import "ProvinceViewController.h"
#import "SecurityViewController.h"
#import "SetViewController.h"
#import "HelpViewController.h"




@interface RootviewConrtoller ()
{
    UINavigationController *nav1;
    UINavigationController *nav2;
    UINavigationController *nav3;
    UINavigationController *nav4;
    UIView *tabbackview;
}
@end

@implementation RootviewConrtoller

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self configTabbar];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changetabbarone:) name:@"changetabbarone" object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changetabbartwo:) name:@"changetabbartwo" object:nil];

    
    
}



- (void) dealloc
{
    NSLog(@"释放%@",[self class]);
}
-(void)configTabbar{
    
    
    PlaceViewController *place=[PlaceViewController new];
  
//    ProvinceViewController *place = [ProvinceViewController new];
    
    place.userOrTourist = self.userOrTourist;
    SecurityViewController *safe =[[SecurityViewController alloc]init];
    SetViewController * set=[SetViewController new];
    HelpViewController *help = [HelpViewController new];
    nav1=[[UINavigationController alloc]initWithRootViewController:place];
    nav1.tabBarItem=[[UITabBarItem alloc]initWithTitle:@"位置" image:[UIImage imageNamed:@"newtabicon01"] selectedImage:[[UIImage imageNamed:@"newtabicon01_on"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    
    
    nav2=[[UINavigationController alloc]initWithRootViewController:safe];
    nav2.tabBarItem=[[UITabBarItem alloc]initWithTitle:@"安全" image:[UIImage imageNamed:@"newtabicon02"] selectedImage:[[UIImage imageNamed:@"newtabicon02_on"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    [nav2 setNavigationBarHidden:YES animated:NO];
    nav3=[[UINavigationController alloc]initWithRootViewController:help];
    nav3.tabBarItem=[[UITabBarItem alloc]initWithTitle:@"自助110" image:[UIImage imageNamed:@"newtabicon03"] selectedImage:[[UIImage imageNamed:@"newtabicon03_on"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    nav4=[[UINavigationController alloc]initWithRootViewController:set];
    nav4.tabBarItem=[[UITabBarItem alloc]initWithTitle:@"设置" image:[UIImage imageNamed:@"newtabicon04"] selectedImage:[[UIImage imageNamed:@"newtabicon04_on"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    [self.tabBar setTintColor:[UIColor whiteColor]];
    [self.tabBar setBackgroundImage:[UIImage imageNamed:@"tabback"]];
    
    //self.selectedIndex=2;
    
    self.viewControllers=[NSArray arrayWithObjects:nav1,nav2,nav3,nav4, nil];
    
    
}

-(void)changetabbarone:(NSNotification *)data{
    //改变tabbbar状态1
    nav1.tabBarItem=[[UITabBarItem alloc]initWithTitle:@"位置" image:[[UIImage imageNamed:@"new2tabicon01"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] selectedImage:[[UIImage imageNamed:@"new2tabicon01_on"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    nav2.tabBarItem=[[UITabBarItem alloc]initWithTitle:@"安全" image:[[UIImage imageNamed:@"new2tabicon02"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] selectedImage:[[UIImage imageNamed:@"new2tabicon02_on"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    [nav2 setNavigationBarHidden:YES animated:NO];
    nav3.tabBarItem=[[UITabBarItem alloc]initWithTitle:@"自助110" image:[[UIImage imageNamed:@"new2tabicon03"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] selectedImage:[[UIImage imageNamed:@"new2tabicon03_on"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    nav4.tabBarItem=[[UITabBarItem alloc]initWithTitle:@"设置" image:[[UIImage imageNamed:@"new2tabicon04"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] selectedImage:[[UIImage imageNamed:@"new2tabicon04_on"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    NSDictionary *dic=data.userInfo;
    self.selectedIndex=[[dic objectForKey:@"key"]integerValue];
    
    //未选中tabbar时的字体颜色设置
    
    
    [nav1.tabBarItem setTitleTextAttributes:@{UITextAttributeTextColor: [UIColor colorWithRed:(CGFloat)21/255 green:(CGFloat)124/255 blue:(CGFloat)244/255 alpha:1.0]} forState:UIControlStateNormal];
    [nav2.tabBarItem setTitleTextAttributes:@{UITextAttributeTextColor: [UIColor colorWithRed:(CGFloat)21/255 green:(CGFloat)124/255 blue:(CGFloat)244/255 alpha:1.0]} forState:UIControlStateNormal];
    [nav3.tabBarItem setTitleTextAttributes:@{UITextAttributeTextColor: [UIColor colorWithRed:(CGFloat)21/255 green:(CGFloat)124/255 blue:(CGFloat)244/255 alpha:1.0]} forState:UIControlStateNormal];
    [nav4.tabBarItem setTitleTextAttributes:@{UITextAttributeTextColor: [UIColor colorWithRed:(CGFloat)21/255 green:(CGFloat)124/255 blue:(CGFloat)244/255 alpha:1.0]} forState:UIControlStateNormal];
    
    [self.tabBar setTintColor:[UIColor colorWithRed:(CGFloat)21/255 green:(CGFloat)124/255 blue:(CGFloat)244/255 alpha:1.0]];
    [self.tabBar setBackgroundImage:[UIImage imageNamed:@"tabbackview2"]];



}
-(void)changetabbartwo:(NSNotification *)data{
    //改变tabbar状态2
    nav1.tabBarItem=[[UITabBarItem alloc]initWithTitle:@"位置" image:[[UIImage imageNamed:@"newtabicon01"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] selectedImage:[[UIImage imageNamed:@"newtabicon01_on"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    nav2.tabBarItem=[[UITabBarItem alloc]initWithTitle:@"安全" image:[[UIImage imageNamed:@"newtabicon02"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] selectedImage:[[UIImage imageNamed:@"newtabicon02_on"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    nav3.tabBarItem=[[UITabBarItem alloc]initWithTitle:@"自助110" image:[[UIImage imageNamed:@"newtabicon03"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] selectedImage:[[UIImage imageNamed:@"newtabicon03_on"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    nav4.tabBarItem=[[UITabBarItem alloc]initWithTitle:@"设置" image:[[UIImage imageNamed:@"newtabicon04"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] selectedImage:[[UIImage imageNamed:@"newtabicon04_on"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    
    [nav1.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor whiteColor]} forState:UIControlStateNormal];
    [nav2.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor whiteColor]} forState:UIControlStateNormal];
    [nav3.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor whiteColor]} forState:UIControlStateNormal];
    [nav4.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor whiteColor]} forState:UIControlStateNormal];

    
    NSDictionary *dic=data.userInfo;
    self.selectedIndex=[[dic objectForKey:@"key"]integerValue];
    [self.tabBar setTintColor:[UIColor whiteColor]];
    [self.tabBar setBackgroundImage:[UIImage imageNamed:@"tabback"]];


}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//- (void)setSelectedIndex:(NSUInteger)selectedIndex
//{
//    [super setSelectedIndex:selectedIndex];
//    if ( ![super respondsToSelector:@selector(tabBar:didSelectItem:)])
//    {
//        return;
//    }
//    if ( 0 == selectedIndex )
//    {
//        [super performSelector:@selector(tabBar:didSelectItem:) withObject:self.tabBar withObject:nav1.tabBarItem];
//    }
//    else if( 1 == selectedIndex )
//    {
//        [super performSelector:@selector(tabBar:didSelectItem:) withObject:self.tabBar withObject:nav2.tabBarItem];
//    }
//    else if( 2 == selectedIndex )
//    {
//        [super performSelector:@selector(tabBar:didSelectItem:) withObject:self.tabBar withObject:nav3.tabBarItem];
//    }
//    else if( 3 == selectedIndex )
//    {
//        [super performSelector:@selector(tabBar:didSelectItem:) withObject:self.tabBar withObject:nav4.tabBarItem];
//    }
//}

//Add by xys:切换标签时显示第一级视图



- (void)setSelectedViewController:(UIViewController *)selectedViewController
{
    [super setSelectedViewController:selectedViewController];
    [selectedViewController.navigationController popToRootViewControllerAnimated:NO];
    UINavigationController * nav = (UINavigationController * )selectedViewController;
    [nav.topViewController.navigationController popToRootViewControllerAnimated:NO];
    NSLog(@"setSelectedViewController");
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
